var part = {
	partCode : "",
	partName : "",
	partType : "",
	quantity : "",
	material : ""
};
init = function(){
};
var _title = [ '批次号','工序名称','工序位置','姓名','物料号', '物料名称', '零件类型', '材料','完成数量','合格数量','处理' ];
var _colModel = [
	{
		name : 'batchNum',
		index : 'batchNum',
		width : '15%',
		align : 'center',
		hidden: false
	}, {
		name : 'procedure.wpName',
		index : 'wpName',
		width : '15%',
		align : "center"
	}, {
		name : 'procedure.wpName',
		index : 'wpName',
		width : '15%',
		align : "center"
	},{
		name : 'workerName',
		index : 'workerName',
		width : '15%',
		align : 'left', 
		editable:true,
           edittype:'text',
           editoptions:{size:20,maxlength:15},
           editrules:{required:true}
	},{
		name : 'partCode',
		index : 'partCode',
		width : '25%',
		align : 'left'
	}, {
		name : 'partName',
		index : 'partName',
		width : '25%',
		align : "left"
	}, {
		name : 'partType',
		index : 'partType',
		width : '15%',
		align : "center"
	}, {
		name : 'material',
		index : 'material',
		width : '15%',
		align : "left"
	},{
		name : 'quantity',
		index : 'quantity',
		width : '15%',
		align : "right",
		editable:true,
           edittype:'text',
           editoptions:{size:20,maxlength:15},
           editrules:{required:true}
	}, {
		name : 'qualifiedQuantity',
		index : 'qualifiedQuantity',
		width : '15%',
		align : "right",
		editable:true,
           edittype:'text',
           editoptions:{size:20,maxlength:15},
           editrules:{required:true}
	}, {
		name : 'id',
		index : 'id',
		width : '15%',
		align : 'center',
		hidden: false,
		formatter: function (value, grid, rows, state) { return "<a href=\"javascript:void(0);\" style=\"color:#f60\" onclick=\"report(" + value + ")\">上报</a>" }
	}
];
var addId = 0;
var lastSel = 1;

/**
 * 点击进入工作单上报页面
 */
function report(id){
	window.location.href="workOrderAction!toReport?id="+id; 
}
checkDuplicate = function(TpartCode) {
	var obj = $("#proTable").jqGrid("getRowData");
	var flag = true;
	if (obj.length > 0) {
		$(obj).each(function() {
			var partCode = this["partCode"];
			if (partCode == TpartCode) {
				alert("物料号重复");
				flag = false;
			}
		});
	}
	return flag;

};

addPro = function(rowData) {
	part.partCode = rowData.partCode;

	part.partName = rowData.partName;
	part.partType = rowData.partType;
	part.material = rowData.material;
	$("#partResult").hide();
	if (checkDuplicate(part.partCode)) {

		jQuery("#proTable").jqGrid('addRowData', addId, part);
		addId = addId + 1;

		window.setTimeout(function() {
			$("#proTable").jqGrid("editCell", addId, 4, true);
		}, 10);
	}

};

onCheckEnter = function(e) {
	var keyCode = null;

	if (e.which)
		keyCode = e.which;
	else if (e.keyCode)
		keyCode = e.keyCode;

	if (keyCode == 13)
		onPartSearch();

};

searchUnreportWorkOrders = function() {
	$("#partResult").show();
	var workerName = document.getElementById("workerName").value;
	var partCode = document.getElementById("partCode").value;
	var batchNum = document.getElementById("batchNum").value;
	var query = "partCode=" + partCode + "&workOrder.workerName=" + workerName+"&workOrder.batchNum="+batchNum;
	var postData = {partCode:partCode,workerName:workerName};
	$("#partTable").jqGrid('GridUnload');
	$("#partTable").jqGrid({
		onSelectRow : function(id) {
			if (id && id !== lastSel) {
				jQuery('#partTable').restoreRow(lastSel);
				lastSel = id;
			}
			var rowData = $("#partTable").jqGrid("getRowData", id);//
			//addPro(rowData);
		},
		url : "workOrderAction!listWorkOrder?"+ query,
		datatype : "json", // 数据来源，本地数据
		mtype : "POST",// 提交方式
		height : 400,// 高度，表格高度。可为数值、百分比或'auto'
		width : 600,// 这个宽度不能为百分比
		autowidth : true,// 自动宽
		scrollrows: true,
		colNames : _title,
		colModel : _colModel,
		postData:postData,
		rownumbers : true,// 添加左侧行号
		// altRows:true,//设置为交替行表格,默认为false
		// sortname:'createDate',
		// sortorder:'asc',
		viewrecords : true,// 是否在浏览导航栏显示记录总数
		rowNum : 15,// 每页显示记录数
		// rowTotal:2000,
		rowList : [ 15, 20, 25 ],// 用于改变显示行数的下拉列表框的元素数组。
		loadonce : true,
		gridview : true,
		viewrecords : true,
		jsonReader : {
			root : "workOrders",
			repeatitems : false
		},
		pager : $('#partPager')
	});
};

getSystemData = function(day) {
	var myDate;
	if (day == undefined) {
		myDate = new Date();
	} else {
		myDate = new Date(day);
	}
	var strDate = myDate.getFullYear() + "/"
			+ ((myDate.getMonth() + 1) < 10 ? "0" : "")
			+ (myDate.getMonth() + 1) + "/"
			+ (myDate.getDate() < 10 ? "0" : "") + myDate.getDate();
	return strDate;
};

loadTable = function() {
	$("#deliveryDate").datepicker({
		changeMonth : true,
		changeYear : true,
		dateFormat : "yy/mm/dd"
	});
	$("#deliveryDate").val(getSystemData());
	
	$("#dueDate").datepicker({
		changeMonth : true,
		changeYear : true,
		dateFormat : "yy/mm/dd"
	});
	$("#dueDate").val(getSystemData());
    var partType = document.getElementById("workerName").value;
    var partCode = document.getElementById("partCode").value;
    var query = "partCode=" + partCode + "&workerName=" + partType;
    $("#partTable").jqGrid('GridUnload');
    $("#partTable").jqGrid({
        onSelectRow : function(id) {
            if (id && id !== lastSel) {
                jQuery('#partTable').restoreRow(lastSel);
                lastSel = id;
            }
            var rowData = $("#partTable").jqGrid("getRowData", id);//
            //addPro(rowData);
        },
        ondblClickRow : function(id){
            //alert(id);
            jQuery('#partTable').editGridRow(id,{height:280,reloadAfterSubmit:true,closeAfterEdit:true,closeOnEscape:true,url:'workOrderAction!saveWorkOrder'});
        },
        url : "workOrderAction!listWorkOrder?" + query,
        datatype : "json", // 数据来源，本地数据
        mtype : "POST",// 提交方式
        height : 420,// 高度，表格高度。可为数值、百分比或'auto'
        width : 600,// 这个宽度不能为百分比
        autowidth : true,// 自动宽
        scrollrows: true,
        colNames : _title,
        colModel : _colModel,
        rownumbers : true,// 添加左侧行号
        // altRows:true,//设置为交替行表格,默认为false
        // sortname:'createDate',
        // sortorder:'asc',
        viewrecords : true,// 是否在浏览导航栏显示记录总数
        rowNum : 15,// 每页显示记录数
        // rowTotal:2000,
        rowList : [ 15, 20, 25 ],// 用于改变显示行数的下拉列表框的元素数组。
        loadonce : true,
        gridview : true,
        viewrecords : true,
        jsonReader : {
            root : "workOrders",
            repeatitems : false
        },
        pager : $('#partPager')
    });
};


/**
 * ‘添加流水’按钮的处理function
 */
addWorkOrder = function(){
	var objList = $("#proTable").jqGrid("getRowData");
	//jsonData = JSON.stringify(jsonData);
	createNewFieldToForm("data_form", "order");
	createNewFieldToForm("data_form", "dataJson");
	/*
	 * createNewFieldToForm("data_form", "dataJson");
	 * 
	 * var jsonData = $("#proTable").jqGrid("getRowData"); //
	 * 通过cookie获取的sm_session_cookie stocktake!warehouseEntry
	 * document.data_form.dataJson.value = JSON.stringify(jsonData);
	 */
	// document.stock_form.warehousingDate.value = JSON.stringify(jsonData);
	document.data_form.order.value = document.getElementById("orderNum").value;
	document.data_form.dataJson.value = JSON.stringify(objList);
	document.data_form.action = "workOrderAction!createNewWorkOrder";
	document.data_form.method = "post";
	document.data_form.submit();
};

